<?php
class Connection {
	public $conn;
	public $isConnected = false;
	
	public function __construct() {
		if (! $this->isConnected) {
			$this->Connect ();
		}
	}
	
	private function Connect() {
		$this->conn = mysql_connect ( _CONNECTION_HOST, _CONNECTION_USER, _CONNECTION_PASS ) or die ( "Bağlantı Hatası!" );
		mysql_select_db ( _CONNECTION_NAME, $this->conn ) or die ( "Veritabanı Seçilemedi!" );
		$this->isConnected = true;
	}
	
	public function CloseConnection() {
		mysql_close ( $this->conn );
	}

}

class Query {
	
	private $Query;
	private $Result;
	private $Row;
	private $Rows = null;
	private $RowCount;
	private $Conn;
	private $Executed = false;
	private $Error;
	
	public function __construct($query, &$link) {
		$this->Conn = $link;
		$this->Query = $query;
		$this->Execute ();
	}
	
	private function Execute() {
		$this->Result = mysql_query ( $this->Query, $this->Conn );
		if ($this->Result) {
			if (is_resource ( $this->Result )) {
				$this->Row ();
				$this->_RowCount ();
				$this->Reset ();
			}
			$this->Executed = true;
		} else {
			$this->Executed = false;
			$this->Error = mysql_error ();
		}
	}
	
	private function Row() {
		$this->Row = mysql_fetch_assoc ( $this->Result );
	}
	
	public function LOOP() {
		return $this->Rows = mysql_fetch_assoc ( $this->Result );
	}
	
	private function _RowCount() {
		$this->RowCount = mysql_num_rows ( $this->Result );
	}
	
	public function RowCount() {
		return $this->RowCount;
	}
	
	public function Field($field) {
		
		if (! isset ( $this->Rows )) {
			return $this->Row [$field];
		} else {
			return $this->Rows [$field];
		}
	
	}
	
	public function Executed() {
		return $this->Executed;
	}
	
	public function Error() {
		return $this->Error;
	}
	
	public function Reset() {
		if ($this->RowCount > 0) {
			mysql_data_seek ( $this->Result, 0 );
		}
		unset ( $this->Rows );
	}
	
	public function dump($pre = true) {
		if ($pre)
			echo '<pre>';
		var_dump ( $this );
		if ($pre)
			echo '</pre>';
	}
	
	public function rsdump($pre = true) {
		if ($pre)
			echo '<pre>';
		print_r ( $this->Rows );
		if ($pre)
			echo '</pre>';
	}

}

?>